package cn.openea.perpartner.crm.web.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.constant.UserConstants;
import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.utils.StringUtils;
import cn.openea.perpartner.crm.web.entity.CrmRole;
import cn.openea.perpartner.crm.web.entity.CrmUserRole;
import cn.openea.perpartner.crm.web.mapper.CrmRoleMapper;
import cn.openea.perpartner.crm.web.mapper.CrmRoleMenuMapper;
import cn.openea.perpartner.crm.web.mapper.CrmUserRoleMapper;
import cn.openea.perpartner.crm.web.service.ICrmRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 角色信息表(CrmRole)表服务实现类
 *
 * @author Weird077
 * @since 2022-12-19 22:30:36
 */
@Service("crmRoleService")
public class CrmRoleServiceImpl extends ServiceImpl<CrmRoleMapper, CrmRole> implements ICrmRoleService {

    @Autowired
    private CrmRoleMenuMapper roleMenuMapper;

    @Autowired
    private CrmUserRoleMapper userRoleMapper;
    /**
     * 根据条件分页查询角色数据
     *
     * @param role 角色信息
     * @return 角色数据集合信息
     */
    @Override
    public List<CrmRole> selectRoleList(CrmRole role) {
        QueryWrapper<CrmRole> wrapper = new QueryWrapper<>();
        wrapper.eq("r.del_flag", "0");
        if (role.getRoleId()!= null) {
            wrapper.eq("r.role_id", role.getRoleId());
        }
        if (StrUtil.isNotBlank(role.getRoleName())) {
            wrapper.eq("r.role_name", role.getRoleName());
        }
        if (StrUtil.isNotBlank(role.getRoleKey())) {
            wrapper.eq("r.role_key", role.getRoleKey());
        }
        if (StrUtil.isNotBlank(role.getStatus())) {
            wrapper.eq("r.status", role.getStatus());
        }
        wrapper.last("order by r.role_sort");
        return baseMapper.selectRoleList(wrapper);
    }
    /**
     * 根据用户ID查询角色列表
     *
     * @param userId 用户ID
     * @return 角色列表
     */
    @Override
    public List<CrmRole> selectRolesByUserId(String userId) {
        List<CrmRole> userRoles = baseMapper.selectRolePermissionByUserId(userId);
        List<CrmRole> roles = selectRoleAll();
        for (CrmRole role : roles)
        {
            for (CrmRole userRole : userRoles)
            {
                if (role.getRoleId().longValue() == userRole.getRoleId().longValue())
                {
                    role.setFlag(true);
                    break;
                }
            }
        }
        return roles;
    }
    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectRolePermissionByUserId(String userId) {
        List<CrmRole> perms = baseMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (CrmRole perm : perms)
        {
            if (StringUtils.isNotNull(perm))
            {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }
    /**
     * 查询所有角色
     *
     * @return 角色列表
     */
    @Override
    public List<CrmRole> selectRoleAll() {
        return list();
    }
    /**
     * 根据用户ID获取角色选择框列表
     *
     * @param userId 用户ID
     * @return 选中角色ID列表
     */
    @Override
    public List<Long> selectRoleListByUserId(String userId) {
        return baseMapper.selectRoleListByUserId(userId);
    }
    /**
     * 通过角色ID查询角色
     *
     * @param roleId 角色ID
     * @return 角色对象信息
     */
    @Override
    public CrmRole selectRoleById(Long roleId) {
        return baseMapper.selectRoleById(roleId);
    }

    /**
     * 校验角色名称是否唯一
     *
     * @param role 角色信息
     * @return 结果
     */
    @Override
    public String checkRoleNameUnique(CrmRole role) {
        Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId();
        CrmRole info = baseMapper.checkRoleNameUnique(role.getRoleName());
        if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    /**
     * 校验角色权限是否唯一
     *
     * @param role 角色信息
     * @return 结果
     */
    @Override
    public String checkRoleKeyUnique(CrmRole role) {
        Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId();
        CrmRole info = baseMapper.checkRoleKeyUnique(role.getRoleKey());
        if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    /**
     * 校验角色是否允许操作
     *
     * @param role 角色信息
     */
    @Override
    public void checkRoleAllowed(CrmRole role) {
        if (StringUtils.isNotNull(role.getRoleId()) && role.isAdmin())
        {
            throw new ServiceException("不允许操作超级管理员角色");
        }
    }

    @Override
    public void checkRoleDataScope(Long roleId) {

    }

    @Override
    public int countUserRoleByRoleId(Long roleId) {
        return 0;
    }

    @Override
    public boolean insertRole(CrmRole role) {
        return save(role);
    }

    @Override
    public boolean updateRole(CrmRole role) {
        return saveOrUpdate(role);
    }

    @Override
    public int updateRoleStatus(CrmRole role) {
        return 0;
    }

    @Override
    public int authDataScope(CrmRole role) {
        return 0;
    }

    @Override
    public int deleteRoleById(Long roleId) {
        return 0;
    }

    @Override
    public int deleteRoleByIds(Long[] roleIds) {
        return 0;
    }

    @Override
    public int deleteAuthUser(CrmUserRole userRole) {
        return 0;
    }

    @Override
    public int deleteAuthUsers(Long roleId, Long[] userIds) {
        return 0;
    }

    @Override
    public int insertAuthUsers(Long roleId, Long[] userIds) {
        return 0;
    }
}

